package com.elvende.web.client.presenter.life;

import com.elvende.web.client.dto.Learning;
import com.elvende.web.client.event.LearningDrillEvent;
import com.elvende.web.client.presenter.Presenter;
import com.elvende.web.client.service.life.LearningServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
public class LearningPresenter implements Presenter {
	 private final LearningServiceAsync rpcService;
	  private final HandlerManager eventBus;
	  private final Display display;
	
	 public LearningPresenter(LearningServiceAsync rpcService, HandlerManager eventBus, Display view) {
		    this.rpcService = rpcService;
		    this.eventBus = eventBus;
		    this.display = view;
	 }
	 
	public interface Display {
		void setData(Learning[] result);	
		HasClickHandlers getStudyButton();
		Widget asWidget();
	}

	public void bind() {
		
	    display.getStudyButton().addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {		    	
		        eventBus.fireEvent(new LearningDrillEvent());
		      }
		    });
		fetchData();
	}
	
   private void fetchData() {
	   rpcService.getRawList(new AsyncCallback<Learning[]>(){
		   public void onSuccess(Learning[] result) {	
			   display.setData(result);
		   }
		   public void onFailure(Throwable caught) {	
			   
		   }
	   });	   
   }

	
	public void go(HasWidgets container) {
	    bind();
	    container.clear();
	    container.add(display.asWidget());
	}
}
